using System;
using System.Xml;

using ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity;


namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.MetaWorkflow
{
	//*********************************************************************
	//
	// XMLAttributesRelationShip Class
	//
	// The XMLAttributesRelationShip class represents the relationships between
	// tow or more entity attributes in a form thath has a filling order in
	// a pre-workflow step of a selection procedure for configuring a product
	//
	// Relatioship is from multi to multi
	//
	//*********************************************************************

	public class XMLAttributesRelationShip : bOSdotNET.XML.Data.XMLObject
	{
		public const string MAIN_NODE	= "RelationShip";

		private const string NAME_ATTRIBUTE		= "Name";

		private String sName;
		private const string FROM_NODE	= "From";
		private const string TO_NODE	= "To";

		/** FIELDS **/

		private ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities vEntitiesFrom;
		private ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities vEntitiesTo;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLAttributesRelationShip() 
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLAttributesRelationShip(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities XMLEntitiesFrom 
		{
			get { return vEntitiesFrom; }
			set { vEntitiesFrom = value; }
		}

		public ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity.XMLEntities XMLEntitiesTo 
		{
			get { return vEntitiesTo; }
			set { vEntitiesTo = value; }
		}


		/** INSTANCE METHODS **/

		override public void Init()
		{
			sName=string.Empty;

			vEntitiesFrom = new Entity.XMLEntities();
			vEntitiesTo = new Entity.XMLEntities();
		}

		/** Parsering method for filling the relationship **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name)
				{
					case XMLAttributesRelationShip.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
				}
			}

			/** find the sub tags witch contains relationship values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLAttributesRelationShip.FROM_NODE:
						vEntitiesFrom.Parse(node.ChildNodes.Item(0));
						break;

					case XMLAttributesRelationShip.TO_NODE:
						vEntitiesTo.Parse(node.ChildNodes.Item(0));
						break;
				} 
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLAttributesRelationShip.MAIN_NODE);
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLAttributesRelationShip.NAME_ATTRIBUTE, sName) );

			bOSdotNET.XML.XMLNode xmlNodeFrom = new bOSdotNET.XML.XMLNode(XMLAttributesRelationShip.FROM_NODE);
			xmlNodeFrom.AddNode ( vEntitiesFrom.GetXMLNode() );
			bOSdotNET.XML.XMLNode xmlNodeTo = new bOSdotNET.XML.XMLNode(XMLAttributesRelationShip.TO_NODE);
			xmlNodeTo.AddNode ( vEntitiesTo.GetXMLNode() );

			xmlNode.AddNode ( xmlNodeFrom );
			xmlNode.AddNode ( xmlNodeTo );

			return xmlNode;
		}

		public void AddEntityAttributeTo(XMLEntityAttribute attribute)
		{
			this.AddEntityAttribute (this.XMLEntitiesTo, attribute);
		}
		public void AddEntityAttributeFrom(XMLEntityAttribute attribute)
		{
			this.AddEntityAttribute (this.XMLEntitiesFrom, attribute);
		}

		private void AddEntityAttribute(XMLEntities xmlEntities, XMLEntityAttribute attribute)
		{
			XMLEntity xmlEntity = null;
			
			try
			{
				xmlEntity = xmlEntities.Get (attribute.EntityRef);
			}
			catch
			{
				xmlEntity = new XMLEntity();
				xmlEntity.Name = attribute.EntityRef;

				xmlEntities.Add (xmlEntity);
			}

			xmlEntity.XMLEntityAttributes.Add (attribute);
		}
	}
}
